package com.ruoyi.storeExpend.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.agent.domain.SysAgent;
import com.ruoyi.agent.mapper.SysAgentMapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.store.domain.SysStore;
import com.ruoyi.store.mapper.SysStoreMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.storeExpend.domain.StoreExpend;
import com.ruoyi.storeExpend.service.IStoreExpendService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商家消费明细Controller
 *
 * @author ruoyi
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/storeExpend/storeExpend")
public class StoreExpendController extends BaseController
{
    @Autowired
    private IStoreExpendService storeExpendService;

    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private SysStoreMapper sysStoreMapper;

    @Autowired
    private SysAgentMapper agentMapper;

    /**
     * 查询商家消费明细列表
     */
//    @PreAuthorize("@ss.hasPermi('storeExpend:storeExpend:list')")
    @GetMapping("/list")
    public TableDataInfo list(StoreExpend storeExpend,
                              @RequestParam(name = "params[beginCreateTime]", required = false) Date beginCreateTime,
                              @RequestParam(name = "params[endCreateTime]", required = false) Date endCreateTime)
    {
        Long deptId = SecurityUtils.getDeptId();
        //查询当前部门的地位
        SysDept sysDept = deptMapper.selectDeptById(deptId);
        List<StoreExpend> list=null;
        if (sysDept==null) {
            throw new ServiceException("用户信息有误");
        }
        String type = sysDept.getType();
        if (type=="0"||type.equals("0")) {
            //说明现在是管理员
            List<Long> deptIds = new ArrayList<>();
            deptIds.add(deptId);
            List<SysStore> sysStores = sysStoreMapper.selectSysStoreByAgentId(0l);
            if (!CollectionUtils.isEmpty(sysStores)) {
                List<Long> collect = sysStores.stream().map(SysStore::getDeptId).collect(Collectors.toList());
               deptIds.addAll(collect);
            }

            storeExpend.setDeptIds(deptIds);

        }
        else if (type=="1"||type.equals("1")) {
            //说明现在是代理商
            List<Long> deptIds = new ArrayList<>();
            deptIds.add(deptId);
            List<SysAgent> agents = agentMapper.selectSysAgentByDeptId(deptId);
            if (CollectionUtils.isEmpty(agents)) {
                throw new ServiceException("代理商信息错误");
            }
            List<SysStore> sysStores = sysStoreMapper.selectSysStoreByAgentId(agents.get(0).getId());
            if (!CollectionUtils.isEmpty(sysStores)) {
                List<Long> collect = sysStores.stream().map(SysStore::getDeptId).collect(Collectors.toList());
                deptIds.addAll(collect);
            }

            storeExpend.setDeptIds(deptIds);


        }
        else if(type=="2"||type.equals("2") ) {
            //当前用户为商铺，只能查看自己商铺的数据
            //根据当前dept_id查询对应的商铺对象，获取商铺的id
            List<SysStore> sysStores = sysStoreMapper.selectSysStoreByDeptId(deptId);
            if (CollectionUtils.isEmpty(sysStores)) {
                throw new ServiceException("当前用户信息有误");
            }
            Long storeId = sysStores.get(0).getId();
            storeExpend.setStoreId(storeId);

        }
        startPage();
        list = storeExpendService.getStoreExpendList(storeExpend,beginCreateTime,endCreateTime);


        return getDataTable(list);
    }

    /**
     * 导出商家消费明细列表
     */
    @PreAuthorize("@ss.hasPermi('storeExpend:storeExpend:export')")
    @Log(title = "商家消费明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StoreExpend storeExpend,
                       @RequestParam(name = "params[beginCreateTime]", required = false) Date beginCreateTime,
                       @RequestParam(name = "params[endCreateTime]", required = false) Date endCreateTime)
    {


        Long deptId = SecurityUtils.getDeptId();
        //查询当前部门的地位
        SysDept sysDept = deptMapper.selectDeptById(deptId);
        List<StoreExpend> list=null;
        if (sysDept==null) {
            throw new ServiceException("用户信息有误");
        }
        String type = sysDept.getType();
        if (type=="0"||type.equals("0") || type=="1" || type.equals("1")) {
            //说明现在是代理商或者管理员
            storeExpend.setDeptId(deptId);

        }
        else if(type=="2"||type.equals("2") ) {
            //当前用户为商铺，只能查看自己商铺的数据
            //根据当前dept_id查询对应的商铺对象，获取商铺的id
            List<SysStore> sysStores = sysStoreMapper.selectSysStoreByDeptId(deptId);
            if (CollectionUtils.isEmpty(sysStores)) {
                throw new ServiceException("当前用户信息有误");
            }
            Long storeId = sysStores.get(0).getId();
            storeExpend.setStoreId(storeId);

        }
        list = storeExpendService.getStoreExpendList(storeExpend,beginCreateTime,endCreateTime);

        ExcelUtil<StoreExpend> util = new ExcelUtil<StoreExpend>(StoreExpend.class);
        util.exportExcel(response, list, "商家消费明细数据");
    }

    /**
     * 获取商家消费明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('storeExpend:storeExpend:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(storeExpendService.selectStoreExpendById(id));
    }

    /**
     * 新增商家消费明细
     */
    @PreAuthorize("@ss.hasPermi('storeExpend:storeExpend:add')")
    @Log(title = "商家消费明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StoreExpend storeExpend)
    {
        return toAjax(storeExpendService.insertStoreExpend(storeExpend));
    }

    /**
     * 修改商家消费明细
     */
    @PreAuthorize("@ss.hasPermi('storeExpend:storeExpend:edit')")
    @Log(title = "商家消费明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StoreExpend storeExpend)
    {
        return toAjax(storeExpendService.updateStoreExpend(storeExpend));
    }

    /**
     * 删除商家消费明细
     */
    @PreAuthorize("@ss.hasPermi('storeExpend:storeExpend:remove')")
    @Log(title = "商家消费明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(storeExpendService.deleteStoreExpendByIds(ids));
    }
}
